package com.study.goal.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.study.common.core.domain.PageQuery;
import com.study.common.core.page.TableDataInfo;
import com.study.common.utils.StringUtils;
import com.study.goal.domain.Goal;
import com.study.goal.domain.bo.GoalBo;
import com.study.goal.domain.vo.GoalVo;
import com.study.goal.mapper.GoalsMapper;
import com.study.goal.service.IGoalService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Collection;

/**
 * 学习目标Service业务层处理
 *
 * @author MT
 * @date 2025-09-03
 */
@RequiredArgsConstructor
@Service
public class GoalServiceImpl implements IGoalService {

    private final GoalsMapper baseMapper;

    /**
     * 查询学习目标
     */
    @Override
    public GoalVo queryById(Long id) {
        GoalVo goalVo = baseMapper.selectVoById(id);
        goalVo.setSonGoalCount(baseMapper.selectCount(new LambdaQueryWrapper<Goal>().eq(Goal::getParentId, goalVo.getId())).intValue());
        goalVo.setSonGoals(
                baseMapper.selectList(new LambdaQueryWrapper<Goal>().eq(Goal::getParentId, goalVo.getId()))
        );
        return goalVo;
    }

    /**
     * 查询学习目标列表
     */
    @Override
    public TableDataInfo<GoalVo> queryPageList(GoalBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<Goal> lqw = buildQueryWrapper(bo);
        Page<GoalVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    @Override
    public TableDataInfo<GoalVo> queryPageListById(Long userId, PageQuery pageQuery) {
        LambdaQueryWrapper<Goal> lqw = Wrappers.lambdaQuery();
        lqw.eq(userId != null, Goal::getUserId, userId);
        lqw.isNull(Goal::getParentId);
        lqw.orderByDesc(Goal::getCreateTime);
        Page<GoalVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);

        //获取子目标数量
        result.getRecords().forEach(goalVo -> {
            goalVo.setSonGoalCount(baseMapper.selectCount(new LambdaQueryWrapper<Goal>().eq(Goal::getParentId, goalVo.getId())).intValue());
        });
        return TableDataInfo.build(result);
    }

    @Override
    public Boolean insertByBo(GoalBo bo, boolean isChild) {
        Goal goal = BeanUtil.toBean(bo, Goal.class);
        goal.setGoalCode(IdUtil.simpleUUID());
        goal.setCreateTime(new Date());
        goal.setUpdateTime(new Date());
        goal.setParentId(isChild ? bo.getParentId() : null);
        validEntityBeforeSave(goal);
        return baseMapper.insert(goal) > 0;
    }

    @Override
    public Boolean updateByBo(GoalBo bo, boolean isChild) {
        return baseMapper.updateById(BeanUtil.toBean(bo, Goal.class)) > 0;
    }

    @Override
    public Boolean deleteWithValidById(Long id) {
        return null;
    }

    private LambdaQueryWrapper<Goal> buildQueryWrapper(GoalBo bo) {
        LambdaQueryWrapper<Goal> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getGoalType() != null, Goal::getGoalType, bo.getGoalType());
        lqw.eq(bo.getUserId() != null, Goal::getUserId, bo.getUserId());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), Goal::getStatus, bo.getStatus());
        return lqw;
    }


    /**
     * 保存前的数据校验
     * 预留后期可以增加判断是否是家长添加的
     */
    private void validEntityBeforeSave(Goal goal) {
        //TODO 做一些数据校验,如唯一约束

    }

}
